package com.dongdong.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dongdong.pojo.TbBrand;
import com.dongdong.service.BeandService;
import com.dongdongshop.configresult.Result;
import com.dongdongshop.em.ResultEnum;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("brand")
public class BrandController {

    @Reference()
    private BeandService beandService;

    @RequestMapping("list")
    public String list(){
        return "admin/brand";
    }

    @RequestMapping("select")
    @ResponseBody
    public Result select(){
        List<TbBrand> tbBrandList = beandService.select();
        if(tbBrandList == null || tbBrandList.size() <= 0){
            return Result.builResult(ResultEnum.ERROR);
        }
        return Result.builResult(ResultEnum.SUCCESS).setResout(tbBrandList);
    }

    @RequestMapping("insert")
    @ResponseBody
    public Result insert(TbBrand tbBrand){
        boolean b = beandService.insert(tbBrand);
        if(b){
            return Result.builResult(ResultEnum.SUCCESS);
        }else {
            return Result.builResult(ResultEnum.ERROR);
        }
    }

    @RequestMapping("delete")
    @ResponseBody
    public Result delete(Long id){
        System.out.println(id);
        boolean b = beandService.delete(id);
        if(b){
            return Result.builResult(ResultEnum.SUCCESS);
        }else {
            return Result.builResult(ResultEnum.ERROR);
        }
    }

    @RequestMapping("toupdate")
    @ResponseBody
    public Result toupdate(@RequestParam("id")Long id){
        TbBrand tbBrand = beandService.selectId(id);
        if(tbBrand == null){
            return Result.builResult(ResultEnum.ERROR);
        }
        return Result.builResult(ResultEnum.SUCCESS).setResout(tbBrand);
    }

    @RequestMapping("update")
    @ResponseBody
    public Result update(TbBrand tbBrand){
        boolean b = beandService.update(tbBrand);
        if(b){
            return Result.builResult(ResultEnum.SUCCESS);
        }else {
            return Result.builResult(ResultEnum.ERROR);
        }
    }
}
